<?php
/**
 * Created by PhpStorm.
 * User: 柳迪
 * Date: 2018/7/12
 * Time: 10:27
 */

namespace app\api\controller;


use think\Controller;
use think\Session;

class Mine extends Controller
{

    public function getDetail()
    {
        if(!Session::has('uid'))
        {
            echo json_encode(['status'=>0,'message'=>'尚未登录，请登录后再试。']);
            die();
        }
        $result=db('users')->field('uname,upic,uemail,utel')->find(Session::get('uid'));
        echo json_encode($result);
        die();
    }

    public function edit()
    {
        if(!Session::has('uid'))
        {
            echo json_encode(['status'=>0,'message'=>'尚未登录，请登录后再试。']);
            die();
        }

        $data='';
        $data['uid']=Session::get('uid');
        if(request()->file('upic'))
        {
            $file=request()->file('upic');
            if($file)
            {
                $info_img=$file->rule('uniqid')->validate(['ext'=>'jpg,png'])->move(ROOT_PATH.'public/static/img/userimg/');
                if($info_img)
                {
                    $result=\db('users')->field('upic')->find(Session::get('uid'));
                    if($result)
                    {
                        if(file_exists(ROOT_PATH.'public/static/img/userimg/'.$result['upic']))
                        {
                            unlink(ROOT_PATH.'public/static/img/userimg/'.$result['upic']);
                        }
                    }
                    $data['upic']=$info_img->getFilename();
                }
                else
                {
                    echo json_encode(['status'=>0,'message'=>'头像上传失败。']);
                    die();
                }
            }
            else
            {
                echo json_encode(['status'=>0,'message'=>'头像上传失败。']);
                die();
            }
        }
        if(input('?utel'))
        {
            $tel=input('utel');
            $isMob="/^1[3-8]{1}[0-9]{9}$/";
            $isTel="/^([0-9]{3,4}-)?[0-9]{7,8}$/";
            if(!preg_match($isMob,$tel) && !preg_match($isTel,$tel))
            {
                echo json_encode(['status'=>0,'message'=>'电话格式错误，请重试。']);
                die();
            }
            else
            {
                $data['utel']=$tel;
            }
        }
        if(input('?uemail'))
        {
            $email=input('uemail');
            $pattern="/([a-z0-9]*[-_.]?[a-z0-9]+)*@([a-z0-9]*[-_]?[a-z0-9]+)+[.][a-z]{2,3}([.][a-z]{2})?/i";
            if(preg_match($pattern,$email))
            {
                $data['uemail']=$email;
            }
            else
            {
                echo json_encode(['status'=>0,'message'=>'邮箱格式错误，请重试。']);
                die();
            }
        }
        try{
            db('users')->update($data);
            echo json_encode(['status'=>1,'message'=>'修改成功。']);
            die();
        }
        catch (\mysqli_sql_exception $exception)
        {
            echo json_encode(['status'=>0,'message'=>'修改失败。']);
            die();
        }
    }

    public function changePwd()
    {
        if(!Session::has('uid'))
        {
            echo json_encode(['status'=>0,'message'=>'尚未登录，请登录后再试。']);
            die();
        }
        if(input('?upwd') && input('?nupwd'))
        {
            $result=db('users')->where(['uid'=>Session::get('uid'),'upassword'=>input('upwd')])->find();
            if($result)
            {
                try{
                    db('users')->update(['uid'=>Session::get('uid'),'upassword'=>input('nupwd')]);
                    echo json_encode(['status'=>1,'message'=>'修改密码成功。']);
                    die();
                }
                catch(\mysqli_sql_exception $exception)
                {
                    echo json_encode(['status'=>0,'message'=>'修改密码失败。']);
                    die();
                }
            }
            else
            {
                echo json_encode(['status'=>0,'message'=>'原密码输入有误。']);
                die();
            }
        }
        else
        {
            echo json_encode(['status'=>0,'message'=>'参数错误。']);
            die();
        }
    }

    public function getFocuslist()
    {
        if(!Session::has('uid'))
        {
            echo json_encode(['status'=>0,'message'=>'尚未登录，请登录后再试。']);
            die();
        }

        $result=db('focus')->where('uid',Session::get('uid'))->select();
        $data='';
        for($i=0;$i<count($result);$i++)
        {
            $data[$i]=db('clubs')->select($result[$i]['cid']);
        }
        echo json_encode($data);
        die();
    }

    public function getShaitulist()
    {
        if(!Session::has('uid'))
        {
            echo json_encode(['status'=>0,'message'=>'尚未登录，请登录后再试。']);
            die();
        }

        $result=db('shaitu')->where('uid',Session::get('uid'))->select();
        echo json_encode($result);
        die();
    }

    public function getLessonlist()
    {
        if(!Session::has('uid'))
        {
            echo json_encode(['status'=>0,'message'=>'尚未登录，请登录后再试。']);
            die();
        }

        $result=db('buy')->where('uid',Session::get('uid'))->select();
        $data='';
        for($i=0;$i<count($result);$i++)
        {
            $data[$i]=db('lessons')->select($result[$i]['lid']);
        }
        echo json_encode($data);
        die();
    }

    public function getActivitieslist()
    {
        if(!Session::has('uid'))
        {
            echo json_encode(['status'=>0,'message'=>'尚未登录，请登录后再试。']);
            die();
        }

        $result=db('enroll')->where('uid',Session::get('uid'))->where('aid','not null')->select();
        var_dump($result);die();
        $data='';
        for($i=0;$i<count($result);$i++)
        {
            $data[$i]=db('activities')->select($result[$i]['aid']);
        }
        echo json_encode($data);
        die();
    }

    public function getMatchlist()
    {
        if(!Session::has('uid'))
        {
            echo json_encode(['status'=>0,'message'=>'尚未登录，请登录后再试。']);
            die();
        }

        $result=db('enroll')->where('uid',Session::get('uid'))->where('mid','not null')->select();
        $data='';
        for($i=0;$i<count($result);$i++)
        {
            $data[$i]=db('match')->select($result[$i]['mid']);
        }
        echo json_encode($data);
        die();
    }

    public function getExerciselist()
    {
        if(!Session::has('uid'))
        {
            echo json_encode(['status'=>0,'message'=>'尚未登录，请登录后再试。']);
            die();
        }

        $result=db('enroll')->where('uid',Session::get('uid'))->where('exerid','not null')->select();
        $data='';
        for($i=0;$i<count($result);$i++)
        {
            $data[$i]=db('exercises')->select($result[$i]['exerid']);
        }
        echo json_encode($data);
        die();
    }

    public function logout()
    {
        if(!Session::has('uid'))
        {
            echo json_encode(['status'=>0,'message'=>'尚未登录，请登录后再试。']);
            die();
        }

        Session::clear();
        echo json_encode(['status'=>1,'message'=>'注销成功。']);
        die();
    }
}